<template>
  <div class="app-container">
    <el-card shadow="never" v-loading="loading">
      <el-row :gutter="5">
        <el-col :span="24" class="main-card-view">
          <el-row>
            <el-card shadow="never" class="top-search-view-card">
              <el-row>
                <el-form :inline="true" :model="searchForm" class="demo-form-inline search-view">
                  <el-form-item label="活动名称">
                    <el-input v-model="searchForm.sysActivityName" placeholder="请输入活动名称"></el-input>
                  </el-form-item>
                  <el-form-item label="用户名称">
                    <el-input v-model="searchForm.memberName" placeholder="请输入用户名称"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="handleSearch" class="search-button">查询</el-button>
                    <!--                                        <el-button type="success" @click="handleReset" class="reset-button">重置</el-button>-->

                  </el-form-item>
                </el-form>
              </el-row>
            </el-card>
          </el-row>

          <el-row>
            <el-card shadow="never">
              <el-table :data="tableData" :stripe="true" style="width: 100%">
                <el-table-column prop="sysActivityName" align="center" label="活动名称" />
                <el-table-column prop="classify" align="center" label="分类" />
                <el-table-column prop="memberName" align="center" label="用户昵称" />
                <el-table-column prop="results" align="center" label="分数" />
                <el-table-column prop="createTime" align="center" label="分数" />
                <el-table-column align="center" label="操作" width="200">
                  <template slot-scope="scope">
                    <el-button type="text" class="table-details-btn"
                      @click="handleDetails(scope.row)"></el-button><!--详情-->
                    <!-- <el-button type="text" class="table-delete-btn"
                      @click="handleDelete(scope.$index, scope.row)"></el-button> -->
                  </template>
                </el-table-column>
              </el-table>
              <XPagination ref="pageInfo" :total="total" :get-data="getList" />
            </el-card>
          </el-row>
        </el-col>
      </el-row>
    </el-card>

    <el-dialog title="答题详情" :visible.sync="dialogVisible" width="80%">
      <el-table :data="list" :stripe="true" style="width: 100%">
        <el-table-column prop="subjectName" align="center" width="600" label="题目名称" />
        <el-table-column prop="type" align="center" label="类型" />
        <el-table-column prop="score" align="center" label="分数" />
        <el-table-column prop="results" align="center" label="答案" />
        <!-- <el-table-column prop="result" align="center" label="用户作答" /> -->
      </el-table>
      <template slot="footer">
        <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import XPagination from "../../components/xpagination/XPagination";
import * as AnswerManger from "../../api/AnswerManger"
import * as classifyManger from "../../api/classifyManger"
import * as formUtils from "../../utils/formUtils";
import { handleTreeData } from "../../utils/tree";
export default {
  name: "list",
  components: { XPagination },
  data () {
    return {
      searchForm: {

      },
      tableData: [],
      treeData: [],
      list: [],
      loading: false,
      dialogVisible: false,
      treeDialog: false,
      total: 0,
      dialogTitle: "添加角色",
      form: {
        timer: []
      },
      editStatus: "add",
      formRules: {
        roleName: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
        description: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
      },
      roleId: '',
      options: [],
      props: {
        value: 'uid',
        label: 'classifyName',
        children: 'children',
        checkStrictly: true,
        emitPath: false
      },
    }
  },
  methods: {
    getList (pageInfo) {
      const param = {
        ...this.searchForm,
        ...pageInfo
      }
      AnswerManger.sumScore(param).then(response => {
        console.log(response)
        this.tableData = response.data.data
        this.total = response.data.data.total
        this.loading = false
      }).catch(error => {
        console.log(error)
      })
    },
    handleDelete (index, data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = {
          uid: data.uid,
        }
        this.loading = true
        AnswerManger.deleteById(param).then(result => {
          this.loading = false
          this.$message.success(result.data.msg)
          this.handleSearch()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    },
    handleSearch () {
      this.$refs.pageInfo.loadData()
    },
    handleDetails (data) {
      this.dialogVisible = true;
      let param = {
        sysActivityUid: data.sysActivityUid,
        memberId: data.memberId
      }
      AnswerManger.queryPage(param).then(result => {
        this.list = result.data.data.rows
        this.list.map(item => {
          if (item.type === '多选题') {
            let arr = JSON.parse(item.results)
            item.results = arr
          }
        })
      }).catch(error => {
        console.log(error)
      })
    },
    handleSave () {
      this.form.startTime = this.form.timer[0]
      this.form.endTime = this.form.timer[1]
      if (this.editStatus === "add") {
        this.handleAdd()
      } else {
        this.handleEdit()
      }
    },
    handleAdd () {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return false
        }
        const param = Object.assign({}, this.form)
        formUtils.filterFormFields(param)
        AnswerManger.insert(param).then(result => {
          this.$message.success(result.data.msg)
          this.dialogVisible = false
          this.handleSearch()
        }).catch(error => {
          this.$message.error(error)
          console.log(error)
        })

      })
    },
    handleEdit () {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return false
        }
        const param = Object.assign({}, this.form)
        formUtils.filterFormFields(param)
        AnswerManger.update(param).then(result => {
          this.$message.success(result.data.msg)
          this.dialogVisible = false
          this.handleSearch()
        }).catch(error => {
          this.$message.error(error)
          console.log(error)
        })
      })
    },
    showEditDialog (index, data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        this.form = Object.assign({}, data)
        this.dialogTitle = "编辑角色"
        this.editStatus = "edit"
      })

    },
    handleChange (value) {
      this.$nextTick(() => {
        this.$refs.cascader.dropDownVisible = false
        this.form.classify = this.$refs.cascader.presentText;
      })
    },
    // 树节点
    getDefaltTreeNode (classifyName) {
      const param = {
        classifyName
      };
      classifyManger.queryOne(param).then(response => {
        this.form.classifyUid = response.data.data.uid
        setTimeout(() => {
          this.$nextTick(() => {
            this.form.classify = this.$refs.cascader.presentText;
            console.log(this.form.classify)
          })
        }, 1000)
      }).catch(error => {
        console.log(error)
      })
    },
    // 树节点
    getTree (classifyName) {
      const param = {
        classifyName
      };
      classifyManger.queryByClassifyName(param).then(response => {
        this.options = handleTreeData(response.data.data)
      }).catch(error => {
        console.log(error)
      })
    },
  },
  mounted () {
    this.handleSearch()
  }
}
</script>

<style scoped lang="scss"></style>
